﻿Imports System.IO
Imports System.Data
Imports System.Data.SqlClient


Public Class frmGen
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnFile As System.Windows.Forms.Button
    Friend WithEvents lstFile As System.Windows.Forms.ListBox
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents txtSource As System.Windows.Forms.RichTextBox
    Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
    Friend WithEvents mnuSave As System.Windows.Forms.ContextMenu
    Friend WithEvents txtNamespace As System.Windows.Forms.TextBox
    Friend WithEvents mnuBind As System.Windows.Forms.MenuItem
    Friend WithEvents mnuInverse As System.Windows.Forms.MenuItem
    Friend WithEvents mnuGenControl As System.Windows.Forms.MenuItem
    Friend WithEvents txtControl As System.Windows.Forms.RichTextBox
    Friend WithEvents mnuOracleScript As System.Windows.Forms.MenuItem
    Friend WithEvents mnuXMLAddnode As System.Windows.Forms.MenuItem
    Friend WithEvents txtConnection As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnFile = New System.Windows.Forms.Button
        Me.txtNamespace = New System.Windows.Forms.TextBox
        Me.lstFile = New System.Windows.Forms.ListBox
        Me.mnuSave = New System.Windows.Forms.ContextMenu
        Me.mnuBind = New System.Windows.Forms.MenuItem
        Me.mnuInverse = New System.Windows.Forms.MenuItem
        Me.mnuGenControl = New System.Windows.Forms.MenuItem
        Me.mnuOracleScript = New System.Windows.Forms.MenuItem
        Me.mnuXMLAddnode = New System.Windows.Forms.MenuItem
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.txtSource = New System.Windows.Forms.RichTextBox
        Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
        Me.txtControl = New System.Windows.Forms.RichTextBox
        Me.txtConnection = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'btnFile
        '
        Me.btnFile.Location = New System.Drawing.Point(8, 14)
        Me.btnFile.Name = "btnFile"
        Me.btnFile.Size = New System.Drawing.Size(120, 24)
        Me.btnFile.TabIndex = 0
        Me.btnFile.Text = "Fill"
        '
        'txtNamespace
        '
        Me.txtNamespace.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtNamespace.Location = New System.Drawing.Point(224, 48)
        Me.txtNamespace.Name = "txtNamespace"
        Me.txtNamespace.Size = New System.Drawing.Size(624, 20)
        Me.txtNamespace.TabIndex = 1
        Me.txtNamespace.Text = "defaultNameSpace"
        '
        'lstFile
        '
        Me.lstFile.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.lstFile.ContextMenu = Me.mnuSave
        Me.lstFile.Location = New System.Drawing.Point(8, 48)
        Me.lstFile.Name = "lstFile"
        Me.lstFile.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended
        Me.lstFile.Size = New System.Drawing.Size(208, 251)
        Me.lstFile.TabIndex = 3
        '
        'mnuSave
        '
        Me.mnuSave.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuBind, Me.mnuInverse, Me.mnuGenControl, Me.mnuOracleScript, Me.mnuXMLAddnode})
        '
        'mnuBind
        '
        Me.mnuBind.Index = 0
        Me.mnuBind.Text = "Bind Control"
        '
        'mnuInverse
        '
        Me.mnuInverse.Index = 1
        Me.mnuInverse.Text = "Inverse"
        '
        'mnuGenControl
        '
        Me.mnuGenControl.Index = 2
        Me.mnuGenControl.Text = "Gen Control"
        '
        'mnuOracleScript
        '
        Me.mnuOracleScript.Index = 3
        Me.mnuOracleScript.Text = "Oracle Script"
        '
        'mnuXMLAddnode
        '
        Me.mnuXMLAddnode.Index = 4
        Me.mnuXMLAddnode.Text = "Gen XML AddNode"
        '
        'txtSource
        '
        Me.txtSource.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtSource.Location = New System.Drawing.Point(224, 80)
        Me.txtSource.Name = "txtSource"
        Me.txtSource.Size = New System.Drawing.Size(624, 224)
        Me.txtSource.TabIndex = 4
        Me.txtSource.Text = ""
        '
        'txtControl
        '
        Me.txtControl.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtControl.Location = New System.Drawing.Point(8, 312)
        Me.txtControl.Name = "txtControl"
        Me.txtControl.Size = New System.Drawing.Size(840, 128)
        Me.txtControl.TabIndex = 5
        Me.txtControl.Text = ""
        '
        'txtConnection
        '
        Me.txtConnection.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtConnection.Location = New System.Drawing.Point(224, 16)
        Me.txtConnection.Name = "txtConnection"
        Me.txtConnection.Size = New System.Drawing.Size(624, 20)
        Me.txtConnection.TabIndex = 6
        Me.txtConnection.Text = "Data Source=(local);Database=dbname;User Id=sa;Password=;"
        '
        'frmGen
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(856, 445)
        Me.Controls.Add(Me.txtConnection)
        Me.Controls.Add(Me.txtNamespace)
        Me.Controls.Add(Me.txtControl)
        Me.Controls.Add(Me.txtSource)
        Me.Controls.Add(Me.lstFile)
        Me.Controls.Add(Me.btnFile)
        Me.Name = "frmGen"
        Me.Text = "Gencode = dataset"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    Private ds As DataSet

    Private Sub FillList(dt as DataTable)
        lstFile.Items.Clear()
        Dim i As Integer
        For i = 0 To dt.Rows.Count - 1
            Dim tbl As String = dt.Rows(i)("TABLE_NAME").ToString()
            Dim str As String = tbl + "-Info"
            lstFile.Items.Add(str)
            str = tbl + "-DAL"
            lstFile.Items.Add(str)
        Next
    End Sub

    Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
        Try
            Dim dt As DataTable = LoadAllTable()
            FillList(dt)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

  
    Private Sub lstFile_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFile.SelectedIndexChanged
        Try

            If lstFile.SelectedItem Is Nothing Then Return

            Dim txt As String = lstFile.SelectedItem.ToString()
            Dim s() As String = txt.Split("-")
            Dim tablename As String = s(0).Trim()

            Dim gen As New GenCode(tablename, txtNamespace.Text, txtConnection.Text)
            Select Case s(1).Trim()
                Case "Info"
                    txtSource.Text = gen.GetInfo()
                Case "DAL"
                    txtSource.Text = gen.GetDAL()
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Function LoadAllTable() As DataTable
        Dim strSQL As String = "SELECT * FROM INFORMATION_SCHEMA.TABLES " _
            + " WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME<>'dtproperties' " _
            + " ORDER BY TABLE_TYPE"
        Dim da As New SqlDataAdapter(strSQL, txtConnection.Text)
        Dim dt As New DataTable
        da.Fill(dt)
        Return dt
    End Function

    Private Sub frmTypedDataset_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ini As New IniFile(System.AppDomain.CurrentDomain.BaseDirectory + "gencode.ini")
        Dim nspace As String = ini.ReadValue("GENCODE", "namespace")
        txtNamespace.Text = nspace
    End Sub

    Private Sub mnuBind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBind.Click
        Dim txt As String = lstFile.SelectedItem.ToString()
        Dim s() As String = txt.Split("-")
        Dim tablename As String = s(0).Trim()
        Dim gen As New GenCode(tablename, txtNamespace.Text, txtConnection.Text)
        txtControl.Text = gen.BindControl()
    End Sub

    Private Sub mnuInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuInverse.Click
        Dim content As String = txtControl.Text.Replace(vbCr, "").Replace(vbLf, "")
        Dim s() As String = content.Split(";")
        Dim txt As String = ""
        For i As Integer = 0 To s.Length - 1
            Dim a() As String = s(i).Split("=")
            If a.Length >= 2 Then
                txt += a(1).Trim() + " = " + a(0).Trim() + ";" + vbCrLf
            End If
        Next
        txtSource.Text = txt
    End Sub

    Private Sub mnuGenControl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGenControl.Click
        Dim txt As String = lstFile.SelectedItem.ToString()
        Dim s() As String = txt.Split("-")
        Dim tablename As String = s(0).Trim()
        Dim gen As New GenCode(tablename, txtNamespace.Text, txtConnection.Text)
        txtControl.Text = gen.GenControl()
    End Sub

    Private Sub mnuOracleScript_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOracleScript.Click
        Dim txt As String = lstFile.SelectedItem.ToString()
        Dim s() As String = txt.Split("-")
        Dim tablename As String = s(0).Trim()
        Dim gen As New GenCode(tablename, txtNamespace.Text, txtConnection.Text)
        txtControl.Text = gen.GenOracleScript()
    End Sub

    Private Sub mnuXMLAddnode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuXMLAddnode.Click
        Dim txt As String = lstFile.SelectedItem.ToString()
        Dim s() As String = txt.Split("-")
        Dim tablename As String = s(0).Trim()
        Dim gen As New GenCode(tablename, txtNamespace.Text, txtConnection.Text)
        txtControl.Text = gen.GenAddNodeXML()
    End Sub
End Class
